package com.zysc.processflow.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateUtil {
    private static SimpleDateFormat getDefaultSimpleDateFormat() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    public static boolean isBlank(String s) {
        return s == null || s.trim().length() == 0;
    }

    public static Calendar strToCalendar(String str) {
        if (isBlank(str)) return null;
        if (str.length() == 10) {
            str = str + " 00:00:00";
        }
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(getDefaultSimpleDateFormat().parse(str));
        } catch (ParseException e) {
            throw new RuntimeException(e.getLocalizedMessage(), e);
        }
        return (Calendar) calendar.clone();
    }

    public static Calendar monthToCalendar(String str) {
        if (str == null || str.equals("")) throw new NullPointerException("date string is null");
        if (str.length() == 7) {
            str = str + "-01 00:00:00";
        }
        if (str.length() == 10) {
            str = str + " 00:00:00";
        }
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(getDefaultSimpleDateFormat().parse(str));
        } catch (ParseException e) {
            throw new RuntimeException(e.getLocalizedMessage(), e);
        }
        return (Calendar) calendar.clone();
    }

    public static String dateToStr(Calendar calendar) {
        return getDefaultSimpleDateFormat().format(calendar.getTime()).substring(0, 10);
    }

    public static String dateToMonth(Calendar calendar) {
        return getDefaultSimpleDateFormat().format(calendar.getTime()).substring(0, 7);
    }

    public static String datatoCalendar(Calendar calendar) {
        return getDefaultSimpleDateFormat().format(calendar.getTime());
    }

    public static String timestampToStr(Calendar calendar) {
        return getDefaultSimpleDateFormat().format(calendar.getTime());
    }

    public static Calendar afterAMonth(Calendar calendar) {
        return afterMonth(calendar);
    }

    public static Calendar afterMonth(Calendar calendar) {
        Calendar c = (Calendar) calendar.clone();
        c.add(Calendar.MONTH, 1);
        return c;
    }

    //由指定日期得到所在周的第一天
    public static String getFirstOfWeek(String dataStr) throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(dataStr));
        int d = 0;
        if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
            d = -6;
        } else {
            d = 2 - cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, d);
        // 所在周开始日期
        String begindate = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        return begindate;
    }

    //取得当前日期的前一天日期
    public static String getCurrentDateBeforeStr() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, -1);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String mDateTime = formatter.format(c.getTime());
        String dataStr = mDateTime.substring(0, 10);
        return dataStr;
    }

    //取得当前日期的前一天日期
    public static Calendar getCurrentDateBefore() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, -1);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        return c;
    }

    //获得所选日期的月第一天
    public static String getMonthFirstDayStr(String datadate) throws Exception {
        Date date = null;
        String monthfisrtday = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        date = format.parse(datadate);

        //创建日历
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        monthfisrtday = format.format(calendar.getTime());
        return monthfisrtday;
    }

    //获得所选日期的月第一天
    public static Calendar getMonthFirstDay(Calendar c) {
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        return c;
    }

    //获得本年第一天日期
    public static String getfirstofyear(String datestr) {
        Date date = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = format.parse(datestr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR), 0, 01);
        String yearfirst = format.format(calendar.getTime());
        return yearfirst;
    }

    //由日期得到所在周为本月第几周
    public static String getweekcount(String datestr) throws ParseException {
        Date date = null;
        String str = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        date = format.parse(datestr);
        int week = (int) Math.ceil(date.getDate() / 7);
        Integer month = date.getMonth() + 1;
        Integer year = date.getYear() + 1900;
        if (date.getDate() < 7) {
            if (date.getDay() != 1) {
                week = 5;
                month = date.getMonth();
            }
        }
        str = year + "年" + month + "月" + "第" + week + "周";
        return str;
    }

    public static Long getMillSecond(String date) {
        long hour = Long.valueOf(date.substring(0, 2)) * 3600 * 1000;
        long min = Long.valueOf(date.substring(3)) * 60 * 2600;
        return hour + min;
    }

    // 日报时间格式处理
    public static Date dayReportTime(String time) {
        String date = time + " 00:00:00:000";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:000");
        Date d = null;
        try {
            d = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

    // 日报时间格式处理（开始）
    public static Date dayReportTimeStart(String time) {
        String date = time + " 00:00:00:000";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:000");
        Date d = null;
        try {
            d = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

    // 日报时间格式处理（结束）
    public static Date dayReportTimeEnd(String time) {
        String date = time + " 23:59:59:000";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:000");
        Date d = null;
        try {
            d = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

    // 月报时间格式处理
    public static Date monthReportTime(String time) {
        String date = time + "-01 00:00:00:000";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:000");
        Date d = null;
        try {
            d = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

    // 月报时间格式处理（开始）
    public static Date monthReportTimeStart(String time) {
        String date = time + "-01 00:00:00:000";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:000");
        Date d = null;
        try {
            d = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

    // 月报时间格式处理（结束）
    public static Date monthReportTimeEnd(String time) {
        String nyrsfm = time + "-01 00:00:00:000";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:000");
        Date d1 = null;
        try {
            d1 =simpleDateFormat.parse(nyrsfm);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d1);
        calendar.add(Calendar.MONTH,1);
        calendar.set(Calendar.SECOND, -1);
        return calendar.getTime();
    }

    // 年报时间格式处理(开始时间)
    public static Date yearReportTimeStart(String time) {
        String date = time + "-01-01 00:00:00:000";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:000");
        Date d = null;
        try {
            d = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

    // 年报时间格式处理(结束时间)
    public static Date yearReportTimeEnd(String time) {
        String date = time + "-12-31 00:00:00:000";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:000");
        Date d = null;
        try {
            d = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

    /**
     * 正则判断输入的日期是否合法
     * 年-月-日   xxxx-xx-xx
     */
    public static boolean isDateNYR(String date) {
        String path = "\\d{4}-\\d{2}-\\d{2}";//定义匹配规回则
        Pattern p = Pattern.compile(path);//实例化Pattern
        Matcher m = p.matcher(date.trim());//验证字符串内容是否合答法
        if (m.matches()) {
            return true;
        }
        return false;
    }

    /**
     * 正则判断输入的日期是否合法
     * 年-月   xxxx-xx
     */
    public static boolean isDateNY(String date) {
        String path = "\\d{4}-\\d{2}";//定义匹配规回则
        Pattern p = Pattern.compile(path);//实例化Pattern
        Matcher m = p.matcher(date.trim());//验证字符串内容是否合答法
        if (m.matches()) {
            return true;
        }
        return false;
    }

    /**
     * 正则判断输入的日期是否合法
     * 年   xxxx
     */
    public static boolean isDateN(String date) {
        String path = "\\d{4}";//定义匹配规回则
        Pattern p = Pattern.compile(path);//实例化Pattern
        Matcher m = p.matcher(date.trim());//验证字符串内容是否合答法
        if (m.matches()) {
            return true;
        }
        return false;
    }

    /**
     * 时间格式化
     *
     * @param time
     * @return
     */
    public static Date dateFormat(String time) {
        String date = time + ":000";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:000");
        Date d = null;
        try {
            d = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

    /**
     * 月份处理
     *
     * @param k
     * @param num
     * @return
     */
    public static String changeMonth(String k, int num) {
        DateFormat df = new SimpleDateFormat("yyyyMM");
        Calendar ct = Calendar.getInstance();
        try {
            ct.setTime(df.parse(k));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        ct.add(Calendar.MONTH, num);
        return df.format(ct.getTime());
    }

    /**
     * 下月月初时间减一天，得到上月最后一天时间
     *
     * @param s
     * @return
     */
    public static String getEndData(String s) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(format.parse(s));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.set(Calendar.SECOND, -1);
        return format.format(calendar.getTime());
    }

}
